package com.clear.command.demo2;

/**
 * 具体命令类：添加文本
 */
public class AddTextCommand implements Command {
    private StringBuilder textEditor;
    private String text;

    public AddTextCommand(StringBuilder textEditor, String text) {
        this.textEditor = textEditor;
        this.text = text;
    }

    @Override
    public void execute() {
        textEditor.append(text);
    }

    @Override
    public void undo() {
        int start = textEditor.length() - text.length();
        int end = textEditor.length();
        textEditor.delete(start, end);
    }
}
